#include <iostream>
#include "../Comm/httplib.h"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_control;

int main()
{
    Server svr;
    Control ctrl;

    svr.set_base_dir("./wwwroot");
    
    // 获取所有题目列表
    svr.Get("/all_questions", [&ctrl](const Request &req, Response &reqs){
        std::string html;
        if (!ctrl.Allquestion(&html)){
            LOG(ERROR, "获取题目列表失败");
        }
        else{
            reqs.set_content(html, "text/html;charset=utf-8");
            LOG(ERROR, "获取题目列表成功");
        } });

    // 用户根据题目编号，获取题目内容
    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &reqs){
        std::string number = req.matches[1];   //获取题目编号
        std::string html;
        if(!ctrl.Numberquestion(number,&html)){
            LOG(ERROR,"获取指定题目失败");
        }
        else
        {
            reqs.set_content(html,"text/html;charset=utf-8");
            LOG(INFO,"获取指定题目成功");
        } });
    //R"()"   raw string  C++提供的原生字符串  ()里面的内容保持原样不改变
    // 根据用户提交的指定题目编号的代码，进行判题  正则表达式
    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &reqs){
        
        std::string number = req.matches[1];
        std::string result_json;
        ctrl.judge(number,req.body,&result_json);
        reqs.set_content(result_json,"application/json;charset=utf-8");
        
        });

    svr.listen("0.0.0.0", 8080); 

    return 0;
}
